﻿<html>
<head>
    <title>判断页面是关闭还是刷新</title>
</head>

<body>
    <script type="text/javascript">
        var obj = {
            "ErrorCode": 0,
            "ErrorMessage": "",
            "ResultTotalCount": 20,
            "QueryResult": [
              {
                  "companyID": 20200,
                  "parentCompanyID": 69,
                  "companyCode": "",
                  "companyName": "厦门市万科物业服务有限公司泉州分公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20199,
                  "parentCompanyID": -1,
                  "companyCode": "",
                  "companyName": "厦门市万科物业服务有限公司晋江金域滨江分公司",
                  "companyAddress": "福建省泉州市晋江市梅岭街道泉安北路1118号万科金域滨江2#3单元一层"
              },
              {
                  "companyID": 20197,
                  "parentCompanyID": -1,
                  "companyCode": "W530104",
                  "companyName": "昆明凯悦物业管理有限公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20196,
                  "parentCompanyID": -1,
                  "companyCode": "W530103",
                  "companyName": "云南德润物业管理有限责任公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20195,
                  "parentCompanyID": -1,
                  "companyCode": "W530102",
                  "companyName": "昆明邦特佳物业管理有限公司云大知城分公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20194,
                  "parentCompanyID": 20076,
                  "companyCode": "W370100",
                  "companyName": "济南万科物业服务有限公司",
                  "companyAddress": "山东省济南市历下区经十东路万科金域中心B座 15楼"
              },
              {
                  "companyID": 20193,
                  "parentCompanyID": 20181,
                  "companyCode": "",
                  "companyName": "福建中联房地产开发集团有限公司（中联）",
                  "companyAddress": ""
              },
              {
                  "companyID": 20190,
                  "parentCompanyID": 20180,
                  "companyCode": "",
                  "companyName": "音西街道清华社区居委会",
                  "companyAddress": ""
              },
              {
                  "companyID": 20189,
                  "parentCompanyID": 20181,
                  "companyCode": "",
                  "companyName": "福清中联置业有限公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20188,
                  "parentCompanyID": 20180,
                  "companyCode": "",
                  "companyName": "融西社区区委会",
                  "companyAddress": ""
              },
              {
                  "companyID": 20187,
                  "parentCompanyID": 20180,
                  "companyCode": "",
                  "companyName": "景江社区居委会",
                  "companyAddress": ""
              },
              {
                  "companyID": 20186,
                  "parentCompanyID": 20181,
                  "companyCode": "",
                  "companyName": "福建中联房地产开发集团有限公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20185,
                  "parentCompanyID": 20181,
                  "companyCode": "",
                  "companyName": "福清蓝天置业有限公司",
                  "companyAddress": "福清市西环北路22号"
              },
              {
                  "companyID": 20184,
                  "parentCompanyID": 20180,
                  "companyCode": "",
                  "companyName": "清华社区居委会",
                  "companyAddress": ""
              },
              {
                  "companyID": 20183,
                  "parentCompanyID": 20180,
                  "companyCode": "",
                  "companyName": "龙辉社区居民委员会",
                  "companyAddress": "福清市龙田镇龙田商贸城"
              },
              {
                  "companyID": 20182,
                  "parentCompanyID": 20180,
                  "companyCode": "",
                  "companyName": "上一村村民委员会",
                  "companyAddress": "福清市上一村街道"
              },
              {
                  "companyID": 20181,
                  "parentCompanyID": 69,
                  "companyCode": "",
                  "companyName": "福建中联房地产开发有限公司",
                  "companyAddress": ""
              },
              {
                  "companyID": 20180,
                  "parentCompanyID": 69,
                  "companyCode": "",
                  "companyName": "福建中联物业服务有限公司",
                  "companyAddress": "福清市西环北路22号"
              },
              {
                  "companyID": 20179,
                  "parentCompanyID": -1,
                  "companyCode": "",
                  "companyName": "莆田市祥和房地产开发有限公司",
                  "companyAddress": "莆田市城厢区霞林居委会孔亭福厦路北侧楼五层"
              },
              {
                  "companyID": 20178,
                  "parentCompanyID": 69,
                  "companyCode": "",
                  "companyName": "泉州市菲莉投资发展有限公司",
                  "companyAddress": "福建省泉州市丰泽区滨海街109号（连捷国际中心）第23层05、06单元"
              }
            ]
        };

        var children = [];

        var data = obj['QueryResult'];

        function toTree(data) {

            var map = {};
            data.forEach(function (item) {
                map[item.companyID] = item;
            });

            var val = [];
            data.forEach(function (item) {

                var parent = map[item.parentCompanyID];
                item.text = item.companyName;

                if (parent) {
                    item.leaf = false;
                    (parent.children || (parent.children = [])).push(item);

                } else {
                    val.push(item);
                    item.leaf = true;
                }
            });

            return val;
        }
        console.log(toTree(data));
    </script>
</body>
</html>
